//
//  PulsVC.m
//  APP_login
//
//  Created by edz on 7/7/21.
//  Copyright © 2021 baicai. All rights reserved.
//

#import "MoreVC.h"
#import "PersonalCenterVC.h"
#import "BZKTools.h"
#import "Masonry.h"

@interface MoreVC () <UIScrollViewDelegate>
@property (nonatomic, strong) UIPageControl *pager;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIImageView * leftView;
@property (nonatomic, strong) UIImageView * currentView;
@property (nonatomic, strong) UIImageView * rightView;
@property (nonatomic, assign) int viewCount;
@property (nonatomic, assign) NSInteger currentViewIndex;
@property (nonatomic, strong) NSArray *imageArry;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) UIView *bcgView;
@property (nonatomic, assign) BOOL isScroll;

@end

@implementation MoreVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"plusPage";
    self.view.backgroundColor = [UIColor whiteColor];
    UIButton *righetButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [righetButton setTitle:@"个人中心" forState:UIControlStateNormal];
    [righetButton addTarget:self action:@selector(personalCenter) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *personalCenterButton = [[UIBarButtonItem alloc] initWithCustomView:righetButton];
    self.navigationItem.rightBarButtonItem = personalCenterButton;
    _isScroll = YES;
    _imageArry = @[@"1", @"2", @"3", @"4", @"5"];
    _currentViewIndex = 0;
    _pager = [[UIPageControl alloc] init];
    [self.view addSubview:_pager];
    [_pager mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(420);
        make.size.mas_equalTo(CGSizeMake(300, 30));
    }];
    _pager.backgroundColor = [UIColor grayColor];
    [_pager setNumberOfPages:5];
    _pager.currentPage = _currentViewIndex;
    [_pager setCurrentPageIndicatorTintColor:[UIColor redColor]];
    [_pager setPageIndicatorTintColor:[UIColor blueColor]];
    [_pager addTarget:self action:@selector(pagChange:) forControlEvents:UIControlEventTouchUpInside];
    
    _bcgView = [UIView new];
    [self.view addSubview: _bcgView];
    [_bcgView mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(30);
        make.size.mas_equalTo(CGSizeMake(300, 400));
     }];
    
    _scrollView = [[UIScrollView alloc] init];
    [_bcgView addSubview:_scrollView];
    [_scrollView mas_makeConstraints:^(MASConstraintMaker *make){
        make.edges.mas_equalTo(self.bcgView).offset(0);
    }];
    _scrollView.pagingEnabled = YES;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.delegate = self;
    
    _contentView = [[UIView alloc] init];
    [_scrollView addSubview:_contentView];
    [_contentView mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.mas_equalTo(-300);
        make.top.mas_equalTo(self.bcgView.mas_top);
        make.height.mas_equalTo(self.bcgView.mas_height);
    }];
    
    _leftView = [[UIImageView alloc] init];
    [_contentView addSubview:_leftView];
    [_leftView mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.mas_equalTo(self.contentView.mas_left).offset(0);
        make.top.equalTo(self.contentView).offset(0);
        make.centerY.equalTo(self.contentView);
        make.width.mas_equalTo(self.bcgView.mas_width);
    }];
    [_leftView setImage:[UIImage imageNamed:_imageArry[4]]];
    
    _currentView = [[UIImageView alloc] init];
    [_contentView addSubview:_currentView];
    [_currentView mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.mas_equalTo(self.leftView.mas_right);
        make.top.equalTo(self.contentView).offset(0);
        make.centerY.equalTo(self.contentView);
        make.width.mas_equalTo(self.bcgView.mas_width);
    }];
    [_currentView setImage:[UIImage imageNamed:_imageArry[0]]];
    
    _rightView = [[UIImageView alloc] init];
    [_contentView addSubview:_rightView];
    [_rightView mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.mas_equalTo(self.currentView.mas_right);
        make.top.equalTo(self.contentView).offset(0);
        make.centerY.equalTo(self.contentView);
        make.width.mas_equalTo(self.bcgView.mas_width);
    }];
    [_rightView setImage:[UIImage imageNamed:_imageArry[1]]];
    
    [_scrollView mas_makeConstraints:^(MASConstraintMaker *make){
        make.right.equalTo(self.rightView.mas_right);
    }];
    [self startTimer];
}

- (void) startTimer{
    _timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
}

- (void) nextPage{
    if (_isScroll) {
        [_contentView mas_updateConstraints:^(MASConstraintMaker *make){
            make.left.mas_equalTo(0);
        }];
        _isScroll = NO;
    }
    [self pageTransition:(_currentViewIndex) % 5 toIndex:(_currentViewIndex+1) % 5];
    _currentViewIndex ++;
    _pager.currentPage = _currentViewIndex % 5;
}

- (void)pagChange:(UIPageControl*) pageControl{
    NSInteger p = [pageControl currentPage];
    if (p == _currentViewIndex % 5) {
        return;
    }
    if (p > _currentViewIndex % 5) {
        [self.timer invalidate];
        [self pageTransition:(_currentViewIndex) % 5 toIndex:p];
        _currentViewIndex = p;
        _pager.currentPage = p;
        [self startTimer];
    }
    if (p < _currentViewIndex % 5) {
        [self.timer invalidate];
        [_scrollView setContentOffset:CGPointMake(300, 0) animated:NO];
        [_currentView setImage: [UIImage imageNamed:_imageArry[(_currentViewIndex) % 5]]];
        [_leftView setImage: [UIImage imageNamed:_imageArry[p]]];
        [UIView animateWithDuration:0.3 animations:^{
            self.scrollView.contentOffset = CGPointMake(0, 0);
        }];
        _currentViewIndex = p;
        _pager.currentPage = p;
        [self startTimer];
    }
}

- (void) pageTransition:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    [_scrollView setContentOffset:CGPointMake(300, 0) animated:NO];
    [_currentView setImage: [UIImage imageNamed:_imageArry[fromIndex]]];
    [_rightView setImage: [UIImage imageNamed:_imageArry[toIndex]]];
    [UIView animateWithDuration:0.3 animations:^{
        self.scrollView.contentOffset = CGPointMake(600, 0);
    }];
}

- (void) scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.timer invalidate];
    if (_isScroll) {
        [_contentView mas_updateConstraints:^(MASConstraintMaker *make){
            make.left.mas_equalTo(0);
        }];
        _isScroll = NO;
    }
    self.scrollView.contentOffset = CGPointMake(300, 0);
    [_currentView setImage: [UIImage imageNamed:_imageArry[(_currentViewIndex) % 5]]];
    [_rightView setImage: [UIImage imageNamed:_imageArry[(_currentViewIndex+1) % 5]]];
    [_leftView setImage: [UIImage imageNamed:_imageArry[(_currentViewIndex + 4) % 5]]];
}

- (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    //右滑
    if (_scrollView.contentOffset.x > 300) {
        _currentViewIndex = (_currentViewIndex + 1) % 5;
    }
    //左滑
    if (_scrollView.contentOffset.x < 300) {
        _currentViewIndex = (_currentViewIndex + 4) % 5;
    }
    //未滑
    if (_scrollView.contentOffset.x == 300) {
        return;
    }
    _pager.currentPage = _currentViewIndex;
    [self startTimer];
}

- (void)personalCenter{
    PersonalCenterVC *personal = [[PersonalCenterVC alloc] init];
    personal.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:personal animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
